/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.google.code.michaelkzhang.bp.InnKeeper;

import net.milkbowl.vault.economy.Economy;

import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.logging.*;

/**
 *
 * @author Michael Zhang
 */
public class InnKeeper extends JavaPlugin {

    /**
     *
     */
    public static final String NAME = "InnKeeper v0.0.1";
    /**
     * The order in which we will search.
     */
    public static final BlockFace[] SEARCH_ORDER = {BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST};
    /**
     * Our logger
     */
    @SuppressWarnings("NonConstantLogger")
    private Logger log;
    /**
     *
     */
    private Economy economy;

    /**
     * {@inheritDoc}
     */
    @Override
    public void onEnable() {
        log = getLogger();
        log.log(Level.INFO, "Enabling {0}", NAME);
        log.info("Hooking into economy..");
        economy = setupEconomy();
        log.log(Level.INFO, "Economy hooked: {0}", economy.getName());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onDisable() {
        log.log(Level.INFO, "Disabling {0}", NAME);
    }

    /**
     * Checks in the player.
     *
     * @param player
     */
    void performCheckIn(Player player) {
    }

    /**
     * Checks out the player.
     *
     * @param player
     */
    void performCheckOut(Player player) {
    }

    /**
     * @return The {@code Economy} to be used
     */
    private Economy setupEconomy() {
        if (getServer().getPluginManager().getPlugin("Vault") == null) {
            throw new RuntimeException(String.format("[%s]: Vault not found; Crashing...", NAME));
        }
        RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
        if (rsp == null) {
            throw new RuntimeException(String.format("[%s]: No economy provider regestered; Crashing...", NAME));
        }
        Economy econ = rsp.getProvider();
        if (econ == null) {
            throw new RuntimeException(String.format("[%s]: Couln't latch into an economy provider; Crashing...", NAME));
        }
        return econ;
    }

    /**
     * @return The economy
     */
    Economy getEconomy() {
        return economy;
    }
}
